package com.itheima.reggie.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Category_分类;
import com.itheima.reggie.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 分类管理
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category_分类 category) {
        log.info("category:{}", category);
        categoryService.save(category);
        return R.success("新增分类成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize) {
        //分页构造器
        Page<Category_分类> pageInfo = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Category_分类> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据sort进行排序
        queryWrapper.orderByAsc(Category_分类::getSort);

        //分页查询
        categoryService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 根据id删除菜品分类信息(用mybatisPlus的方法)
     * @param id
     * @return
     */
//    @DeleteMapping
//    public R<String> delete(long id) {
//        log.info("删除分类，id为：{}",id);
//
//        boolean b = categoryService.removeById(id);
//        if (b) {
//            return R.success("分类信息删除成功！");
//        }
//        return R.error("分类信息删除失败！");
//
//    }

    /**
     * 根据id删除分类(用自己写的方法)
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id) {
        log.info("删除分类，id为：{}", id);

        //categoryService.removeById(id);
        categoryService.remove(id);

        return R.success("分类信息删除成功");
    }


    /**
     * 根据id修改菜单分类信息
     *
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category_分类 category) {
        log.info(category.toString());

        categoryService.updateById(category);
        return R.success("修改分类信息成功!");
    }

    //    @GetMapping("/list")
//    public R<List<Category>> getByType(int type) {
//        log.info(type + "");
//
//        List<Category> list = categoryService.list(
//                Wrappers.<Category>lambdaQuery()
//                        .eq(type == 1 || type == 0, Category::getType, type)
//        );
//
//        return R.success(list);
//    }

    /**
     * 根据类型查询菜品分类(菜品是1，套餐是2)
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category_分类>> getByType(Category_分类 category) {
        log.info(category.toString());

        List<Category_分类> list = categoryService.list(
                Wrappers.<Category_分类>lambdaQuery()
                        .eq(category.getType() != null, Category_分类::getType, category.getType())
                        .orderByAsc(Category_分类::getSort).orderByDesc(Category_分类::getUpdateTime)
        );

        return R.success(list);
    }
}  